/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.dailyexpress.requestprocess2;

import com.elcom.emocbc.cbcresponseparser.ResponseParser;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.CbcMessageResponse;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.CbcMessage;
import com.elcom.emocbc.cbcresponseparser.cbcresponse.structure.CbcMessageStatus;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
public class SendMessageRequestProcessor extends RequestProcessor {

    private final ResponseParser<CbcMessageResponse> parser = new ResponseParser<CbcMessageResponse>();
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public SendMessageRequestProcessor(HttpServletRequest request, HttpServletResponse response) {
        super(request, response);
    }

    @Override
    public void process() throws Exception {
        logger.debug("[SMSCB {}]Processing request {}", smscbCenterId, request.getRequestURI());
        String bsc = request.getParameter("bsclist");
        Long messageId = MessageIdCache.getInstance().put(bsc);
        returnResponse(messageId);
        logger.debug("[SMSCB {}]>>> SendMessageResponse returned with seq {}", smscbCenterId, messageId);
    }

    private void returnResponse(long messageId) throws IOException {
        CbcMessageResponse cbcMessageResponse = new CbcMessageResponse();
        cbcMessageResponse.setRequest("sendmessage");
        cbcMessageResponse.setStatus("SUCCESS");
        CbcMessageStatus cbcMessageStatus = new CbcMessageStatus(0, "To be submited");
        CbcMessage cbcMessage = new CbcMessage(String.valueOf(messageId), cbcMessageStatus);
        cbcMessageResponse.setCbcMessage(cbcMessage);

        response.getWriter().println(parser.marshall(cbcMessageResponse));
    }
}
